package com.chinasoft.carWithuserservice.service.impl;

import com.chinasoft.carWithuserservice.mapper.CarWithUserMapper;
import com.chinasoft.carWithuserservice.service.CarWithUserService;
import com.chinasoft.feignclient.client.CarClient;
import com.chinasoft.feignclient.client.UserClient;
import com.chinasoft.ms.dto.CarDTO;
import com.chinasoft.ms.dto.CarWithUserDTO;
import com.chinasoft.ms.pojo.Car;
import com.chinasoft.ms.pojo.CarWithUser;
import com.chinasoft.ms.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class CarWithUserServiceImpl implements CarWithUserService {

    @Autowired
    private  CarWithUserMapper carWithUserMapper;

    @Autowired
    UserClient userClient;

    @Autowired
    CarClient carClient;

    @Override
    public PageInfo<CarWithUserDTO> queryByPage(Integer pageNum, Integer pageSize, Long carId, Long userId) {
        PageHelper.startPage(pageNum, pageSize);
        CarWithUser query = new CarWithUser();
        query.setCarId(carId);
        query.setUserId(userId);  // 这里会自动使用Controller传入的用户ID
        int offset = (pageNum - 1) * pageSize;
        List<CarWithUser> list = carWithUserMapper.selectPage(carId, userId, offset, pageSize);

        // 转换为DTO列表
        List<CarWithUserDTO> dtoList = list.stream().map(item -> {
            CarWithUserDTO dto = new CarWithUserDTO();
            BeanUtils.copyProperties(item, dto);

            // 获取关联信息
            CarDTO car = carClient.selectById(item.getCarId());
            User user = userClient.getUserWithId(item.getUserId());

            dto.setModel(car != null ? car.getModel() : "");
            dto.setNickName(user != null ? user.getNickName() : "");

            return dto;
        }).collect(Collectors.toList());

        int total = carWithUserMapper.selectCount(carId, userId);

        PageInfo<CarWithUserDTO> pageInfo = new PageInfo<>();
        pageInfo.setList(dtoList);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);

        return pageInfo;
    }

    @Override
    public boolean saveOrUpdate(CarWithUser carWithUser) {
        return false;
    }

    @Override
    public int deleteById(Long id) {
        return carWithUserMapper.deleteById(id);
    }

    @Override
    public int update(CarWithUserDTO carWithUser) {
        System.out.println("carWithUser:"+carWithUser);
        return carWithUserMapper.update(carWithUser);
    }

    @Override
    public int getRoleUserWithId(Long userId) {
        return userClient.getUserroleWithId(userId);
    }

    @Override
    public int insert(CarWithUserDTO carWithUser) {
        return carWithUserMapper.insert(carWithUser);
    }

    @Override
    public int deleteByCarIdAndUserId(Long CarId) {
        return carWithUserMapper.deleteByCarId(CarId);
    }
}
